package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Shop;
import cn.wolfcode.trip.base.service.IArticleService;
import cn.wolfcode.trip.base.service.IShopService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shop")
public class ShopController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IShopService shopService;

    @PostMapping(value = "/shopCat")
    public JSONResult saveShopCatOrder(Long articleId,int number,int totalCost){
         JSONResult result=new JSONResult();
         try {
            shopService.saveShopCatOrder(articleId,number,totalCost);
         }catch (Exception e){
             e.printStackTrace();
             result.mark(e.getMessage());
         }
         return result;
    }

    @GetMapping(value = "/{userId}/{articleId}")
    public Shop getArticleDetail(@PathVariable("userId") Long userId, @PathVariable("articleId") Long articleId){
        return shopService.selectShopCatByUserIdAndArticleId(userId,articleId);
    }


    @PutMapping(value = "/{articleId}/{resultCost}")
    public JSONResult resultShopCatOrder(@PathVariable("articleId") Long articleId,@PathVariable("resultCost") Integer resultCost){
        JSONResult result=new JSONResult();
        try {
            shopService.resultShopCatOrder(articleId,resultCost);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @GetMapping(value = "/{userId}")
    public List<Shop> getArticleDetail(@PathVariable("userId") Long userId){
        return shopService.getArticleDetailByUserId(userId);
    }

    @GetMapping(value = "/all/{userId}")
    public List<Shop> getall(@PathVariable("userId") Long userId){
        return shopService.getArticleByUserId(userId);
    }



}
